package com.jiazhong.mingxing.project.bookstore.front.controller;

import com.jiazhong.mingxing.project.bookstore.commons.util.JsonResult;
import com.jiazhong.mingxing.project.bookstore.commons.util.ResultTool;
import com.jiazhong.mingxing.project.bookstore.front.service.ShoppingCartService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/shopping_cart")
@Tag(description = "购物车接口", name = "shoppingCartController")
public class ShoppingCartController {
    @Resource
    private ShoppingCartService shoppingCartService;

    @Operation(
            description = "添加购物车API",
            parameters = {
                    @Parameter(name = "book_id", required = true, description = "需要加入到购物车的图书ID"),
                    @Parameter(name = "token", required = true,
                            description = "用户令牌，以token形式保存,需解析", in = ParameterIn.HEADER)
            },
            responses = {
                    @ApiResponse(description = "成功加入购物车", responseCode = "200"),
                    @ApiResponse(description = "用户没有登陆", responseCode = "401"),
                    @ApiResponse(description = "其他错误", responseCode = "500")
            }
    )
    @PostMapping("/add")
    public JsonResult addCart(@RequestParam("book_id") String bookId, HttpServletRequest request) {
        log.info("加入购物车成功");
        return shoppingCartService.addCart(bookId, request);
    }

    @GetMapping("/find_cart_user")
    public JsonResult findCartByUserId(HttpServletRequest request) {
        return shoppingCartService.findCartByUserId(request);
    }

    // -1
    @PutMapping("/change_number_1")
    public JsonResult updateCartNumber1(@RequestParam("book_id") String bookId, HttpServletRequest request) {
        return shoppingCartService.updateCartNumber1(bookId, request);
    }

    // +1
    @PutMapping("/change_number_2")
    public JsonResult updateCartNumber2(@RequestParam("book_id") String bookId, HttpServletRequest request) {
        return shoppingCartService.updateCartNumber2(bookId, request);
    }

    @PutMapping("/change_number_3")
    public JsonResult updateCartNumber3(@RequestParam("book_id") String bookId,
                                        HttpServletRequest request,
                                        @RequestParam("number") int number) {
        return shoppingCartService.updateCartNumber3(bookId, request, number);
    }

    @DeleteMapping("/remove_cart")
    public JsonResult removeCartByBookId(@RequestParam("book_id") String bookId, HttpServletRequest request) {
        return shoppingCartService.removeCartByBookId(bookId, request);
    }

    @DeleteMapping("/remove_cart_batch")
    public JsonResult removeCartBatch(@RequestParam("book_ids") String bookIds, HttpServletRequest request) {
        log.info("bookids:{}", bookIds);
        return shoppingCartService.removeCartByBatch(bookIds, request);
    }
}
